package pub.developers.forum.portal.controller.admin;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pub.developers.forum.api.model.PageRequestModel;
import pub.developers.forum.api.model.PageResponseModel;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.api.request.AdminBooleanRequest;
import pub.developers.forum.api.request.article.ArticleAddTypeRequest;
import pub.developers.forum.api.request.article.ArticleAdminPageRequest;
import pub.developers.forum.api.request.article.ArticleAdminTypePageRequest;
import pub.developers.forum.api.request.article.ArticleDeleteRequest;
import pub.developers.forum.api.response.article.ArticleQueryTypesResponse;
import pub.developers.forum.api.response.article.ArticleUserPageResponse;
import pub.developers.forum.api.response.inform.InformPageResponse;
import pub.developers.forum.api.service.ArticleApiService;
import pub.developers.forum.api.service.PostsApiService;
import pub.developers.forum.common.constant.Constant;
import pub.developers.forum.portal.support.WebUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;



@RestController
@RequestMapping("/admin-rest/article") //管理员管理文章

public class AdminRestArticleController {

    @Resource
    private ArticleApiService articleApiService;

    @Resource
    private PostsApiService postsApiService;


    //ResultModel 自定义的返回值的类

    @PostMapping("/all-type") //获取全部的文章类型
    public ResultModel<List<ArticleQueryTypesResponse>> allAdminTypes(HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request)); //将token存储在请求对象中,判断用户是否登录


        return articleApiService.queryAdminTypes();
    }

    @PostMapping("/all-ban")
    public ResultModel getArticleByStatus(@RequestBody PageRequestModel pageRequestModel,HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY,WebUtil.cookieGetSid(request));

        return articleApiService.queryBan(pageRequestModel);
    }

    @PostMapping("/all-reported")
    public ResultModel<PageResponseModel<InformPageResponse>> getReportedArticle(@RequestBody PageRequestModel pageRequestModel, HttpServletRequest request) throws Exception {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY,WebUtil.cookieGetSid(request));

        return articleApiService.queryReported(pageRequestModel);
    }

    @PostMapping("/all-filter")
    public ResultModel getFilterArticle(@RequestBody PageRequestModel pageRequestModel,HttpServletRequest request) throws Exception {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY,WebUtil.cookieGetSid(request));

        return articleApiService.queryFilterPosts(pageRequestModel);
    }



    /*
        文章管理的条件查询 , 参数是查询条件
        {
        "filter":{
            "typeId":"", Long文章类型id
            "auditState":"",String 文章审核状态
            "official":"", Boolean 文章是否为官方
            "top":"", Boolean 文章是否为置顶
            "marrow":"",  Boolean 文章是否是加精
            "userId":"", Long 作者id
            "title":"" String 文章标题
        },
        "pageNo":"1", //当前页
        "pageSize":"10"//每页条数
     */

    @PostMapping("/page")
    public ResultModel<PageResponseModel<ArticleUserPageResponse>> page(@RequestBody PageRequestModel<ArticleAdminPageRequest> pageRequestModel
            , HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return articleApiService.adminPage(pageRequestModel);
    }


    /*
     帖子是否设置为置顶状态
     {
        "id" : "" 帖子id
        "is" : "" 帖子是否设置为置顶
     }
     */
    @PostMapping("/top")
    public ResultModel top(@RequestBody AdminBooleanRequest booleanRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));//判断当前登录状态



        return articleApiService.adminTop(booleanRequest);
    }




    /*
    帖子是否设置为官方帖子
    {
        "id" : "" 帖子id
        "is" : "" 帖子是否设置为官方帖子
     }
     */
    @PostMapping("/official")
    public ResultModel official(@RequestBody AdminBooleanRequest booleanRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));//判断登录状态


        return articleApiService.adminOfficial(booleanRequest);
    }


    /*
    帖子是否设置为加精帖子
    {
        "id" : "" 帖子id
        "is" : "" 帖子是否设置为加精帖子
    }
     */
    @PostMapping("/marrow")
    public ResultModel marrow(@RequestBody AdminBooleanRequest booleanRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return articleApiService.adminMarrow(booleanRequest);
    }



    /*
    帖子审核是否通过
    {
        "id": 帖子id
        "is" : 审核是否通过
    }
     */
    @PostMapping("/audit-state")
    public ResultModel auditState(@RequestBody AdminBooleanRequest booleanRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return postsApiService.auditState(booleanRequest);
    }

    @PostMapping("/report-review")
    public ResultModel reportReview(@RequestBody AdminBooleanRequest request ,HttpServletRequest servletRequest){
        servletRequest.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(servletRequest));

        return articleApiService.reportReview(request);
    }




    /*
    文章类别(条件查询,分页查询,模糊查询)
    PageRequestModel<ArticleAdminTypePageRequest> pageRequestModel 条件,分页,以及模糊查询的参数
     */
    @PostMapping("/type-page")
    public ResultModel<PageResponseModel<ArticleQueryTypesResponse>> typePage(@RequestBody PageRequestModel<ArticleAdminTypePageRequest> pageRequestModel, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));//判断登录状态

        return articleApiService.typePage(pageRequestModel);
    }


    /*
       文章类别审核是否通过
       {
           "id" : 文章类别id
           "is" : 文章类别审核是否通过
       }
     */
    @PostMapping("/type-audit-state")
    public ResultModel typeAuditState(@RequestBody AdminBooleanRequest booleanRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return articleApiService.typeAuditState(booleanRequest);
    }


    /*
    增加文章类别
    ArticleAddTypeRequest articleAddTypeRequest 文章类别参数
    description: 类别描述
    name:类别名称
    scope:类别作用域(官方或者非官方)
     */

    @PostMapping("/type-add")
    public ResultModel addType(@RequestBody ArticleAddTypeRequest articleAddTypeRequest, HttpServletRequest request) {
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY, WebUtil.cookieGetSid(request));

        return articleApiService.addType(articleAddTypeRequest);
    }


    //官方删除
    // 帖子
    @PostMapping("/delete-articles")
    public ResultModel deleteArticle(@RequestBody ArticleDeleteRequest articleDeleteRequest , HttpServletRequest request) {
        //验证登录状态
        request.setAttribute(Constant.REQUEST_HEADER_TOKEN_KEY , WebUtil.cookieGetSid(request));
        //////System.out.println(articleDeleteRequest);
        return articleApiService.delete(articleDeleteRequest);
    }
}
